﻿using System;
using System.Collections;
using System.Threading;

namespace HttpModuleAccessControl
{
    public class Roles : CollectionBase, IEnumerable
    {
        static ReaderWriterLock rwlock = new ReaderWriterLock();

        /// <summary>
        /// Default constructor. Do nothing in this version.
        /// </summary>
        public Roles()
        {
            // do nothing
        }

 
        /// <summary>
        /// Get all roles from database, which have Roles with Permission
        /// </summary>
        /// <param name="loadRolesFromDB"></param>
        public Roles(bool loadRolesFromDB)
        {
            Roles rles = Database.GetRoles();

            rwlock.AcquireWriterLock(1000);

            if (List.Count >= 0)
                List.Clear();

            foreach (Role rle in rles)
                List.Add(rle);

            rwlock.ReleaseWriterLock();
        }

        /// <summary>
        /// Get all roles from the user ID. Useless in this version.
        /// </summary>
        /// <param name="UserID">User ID</param>
        public Roles(string UserID)
        { 
            // do nothing in this version
        }

         /// <summary>
        /// Add a role member
        /// </summary>
        /// <param name="role"></param>
        public void Add(Role role)
        {
            for (int i = 0; i < List.Count; i++)
            {
                if (((Role)List[i]).RoleID == role.RoleID)
                    return;
            }
            rwlock.AcquireWriterLock(1000);
            List.Add(role);
            rwlock.ReleaseWriterLock();
        }

        // basic method
        public void Remove(int index)
        {
            if (index > Count - 1 || index < 0)
            {
                //throw new Exception("Index out of bounds");
                //return without an exception in this version.
                return;
            }

            rwlock.AcquireWriterLock(1000);
            List.RemoveAt(index);
            rwlock.ReleaseWriterLock();
        }

        /// <summary>
        /// Return a Role based on its index position in the Role list.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public Role Item(int Index)
        {
            rwlock.AcquireReaderLock(500);
            Role r = (Role)List[Index];
            rwlock.ReleaseReaderLock();
            return r;
        }

        public int FindRoleListIndex(int RoleID)
        {
            int _answer = -1;

            rwlock.AcquireReaderLock(500);
            for (int i = 0; i < List.Count; i++)
            {
                Role rle = (Role)List[i];
                if (rle.RoleID == RoleID)
                {
                    _answer = i;
                    break;
                }
            }
            rwlock.ReleaseReaderLock();

            return _answer;
        }

        // Not use in this version.
        //public bool Contains(int RoleID)
        //{
        //    bool _answer = false;

        //    for (int index = 0; index < Count; index++)
        //    {
        //        if (((Role)List[index]).RoleID == RoleID)
        //        {
        //            _answer = true;
        //            break;
        //        }
        //    }

        //    return _answer;
        //}

        // Not use in this version.
        //public bool Contains(string Rolename)
        //{
        //    bool _answer = false;
        //    for (int index = 0; index < Count; index++)
        //    {
        //        if (((Role)List[index]).RoleName.ToUpper().Trim() == Rolename.ToUpper().Trim())
        //        {
        //            _answer = true;
        //            break;
        //        }
        //    }
        //    return _answer;
        //}



 

    }
}
